//
//  LTBaseViewController.swift
//  textweibo
//
//  Created by 李付涛 on 2017/3/17.
//  Copyright © 2017年 江苏岳创信息科技有限公司. All rights reserved.
//

import UIKit

class LTBaseViewController: UIViewController {

    
    /// 是否登录标示
    var isLogin = true
 
    /// 标记上拉加载更多
    var isPullUp = false
    
    var tableView:UITableView?
    
    var refreshController:UIRefreshControl?
    
    
    lazy var navigationbar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: UIScreen.cz_screenWidth(), height: 64))
    lazy var naviItem = UINavigationItem()
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        
        loadData()
    }

    ///获取数据
    func loadData() {
        
    }
    
    ///重写title的didSet
    override var title: String? {
        didSet{
            naviItem.title = title
        }
    }
}

extension LTBaseViewController{
    
    func setupUI() {
        self.view.backgroundColor = UIColor.cz_random()
        automaticallyAdjustsScrollViewInsets = false
        setNavigaTionBar()
        isLogin ? setTableView() : setNoLoginUI()
    }
    
    
    private func setNoLoginUI() {
        
        let backView = LTvisitorView(frame: view.bounds)
       // backView.backgroundColor = UIColor.cz_random()
        view.insertSubview(backView, belowSubview: navigationbar)
        
    }
    
    
    ///设置tableView
   private func setTableView(){
        tableView = UITableView(frame: CGRect(x: 0, y: 64, width: view.bounds.width, height: view.bounds.height-49-64))
    view.insertSubview(tableView!, belowSubview: navigationbar)
    tableView?.delegate = self
    tableView?.dataSource = self
   // tableView?.contentInset = UIEdgeInsetsMake(navigationbar.bounds.height, 0, 0, tabBarController?.tabBar.bounds.height ?? 49)
    //实例化刷新控件
    refreshController = UIRefreshControl()
    
    tableView?.addSubview(refreshController!)
    
    refreshController?.addTarget(self, action: #selector(loadData), for: .valueChanged)
    
    }
    
    ///设置导航条
   private func setNavigaTionBar() {
        view.addSubview(navigationbar)
        navigationbar.items = [naviItem]
        navigationbar.tintColor = UIColor.cz_color(withHex: 0xF6F6F6)
        navigationbar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.darkGray]
    }
    
    
}
extension LTBaseViewController:UITableViewDataSource,UITableViewDelegate{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        
        let row = indexPath.row
        
        let section = tableView.numberOfSections - 1
        
        if row < 0 || section < 0 {
            return
        }
        
        let count = tableView.numberOfRows(inSection: section)
        
        
        if row == (count - 1) && isPullUp == false {
            print("!!!!")
        }
        
    }
    
}
